<?php
/**
 * Random Number
 *
 * @param int $length
 * @return string
 */
function getRandNum($length = 8) {
    $salt = substr(uniqid(rand()), -$length);
    return $salt;
}

/**
 * Random String
 *
 * @param int $length
 * @return string
 * */
function getRandStr($length = 8) {
    $Result = "";
    $str = "1234567890qwertyuiopasdfghjklzxcvbnm";
    $len = strlen($str);
    for ($i = 0; $i < $length; $i++) {
        $Result .= substr($str, rand(0, $len), 1);
    }
    return $Result;
}

/**
 * GB2312 to UTF-8
 */
function encode_gb2utf($subject) {
    return _encode($subject, 'GB2312', 'UTF-8');
}

function encode_utf2gb($subject) {
    return _encode($subject, 'UTF-8', 'GB2312');
}

function encode_utf2gbk($subject) {
    return _encode($subject, 'UTF-8', 'GBK');
}

function _encode($str, $src, $obj, $type=1) {
    switch ($type) {
        case 0:
            $str = iconv($src, $obj, $str);
            break;
        case 1:
            $str = mb_convert_encoding($str, $obj, $src);
            break;
    }
    return $str;
}

/**
 * Chinese substr
 */
function cn_substr($sourcestr, $cutlength) {
    $returnstr = '';
    $i = 0;
    $n = 0;
    $str_length = strlen($sourcestr);
    while (($n < $cutlength) and ($i <= $str_length)) {
        $temp_str = substr($sourcestr, $i, 1);
        $ascnum = Ord($temp_str);
        //if ASCII bit > 224
        if ($ascnum >= 224) {
            $returnstr = $returnstr . substr($sourcestr, $i, 3);
            $i = $i + 3;
            $n++;
        } elseif ($ascnum >= 192) {
            $returnstr = $returnstr . substr($sourcestr, $i, 2);
            $i = $i + 2;
            $n++;
        } elseif ($ascnum >= 65 && $ascnum <= 90) {
            $returnstr = $returnstr . substr($sourcestr, $i, 1);
            $i = $i + 1;
            $n++;
        } else {
            $returnstr = $returnstr . substr($sourcestr, $i, 1);
            $i = $i + 1;
            $n = $n + 0.5;
        }
    }
    if ($sourcestr != $returnstr) {
        $returnstr = $returnstr . "...";
    }
    return $returnstr;
}

/**
 * English substr
 */
function smart_substr($string, $length = 80, $etc = '...', $break_words = false, $intercept_p = true) {
    if ($length == 0)
        return '';

    if ($intercept_p) {
        $matches = array();
        if (preg_match("/<p[^>]*>(.*)<\/p>/i", $string, $matches)) {
            $string = $matches[1];
        }
    }

    if (strlen($string) > $length) {

        $length -= min($length, strlen($etc));
        if (!$break_words && !$middle) {
            $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length + 1));
        }
        return strip_tags(substr($string, 0, $length)) . $etc;
    } else {
        return $string;
    }
}

function html_substr( $s, $srt, $len = NULL, $strict=false, $suffix = NULL )
{
	if ( is_null($len) ){ $len = strlen( $s ); }
	
	$f = 'static $strlen=0; 
			if ( $strlen >= ' . $len . ' ) { return "><"; } 
			$html_str = html_entity_decode( $a[1] );
			$subsrt   = max(0, ('.$srt.'-$strlen));
			$sublen = ' . ( empty($strict)? '(' . $len . '-$strlen)' : 'max(@strpos( $html_str, "' . ($strict===2?'.':' ') . '", (' . $len . ' - $strlen + $subsrt - 1 )), ' . $len . ' - $strlen)' ) . ';
			$new_str = substr( $html_str, $subsrt,$sublen); 
			$strlen += $new_str_len = strlen( $new_str );
			$suffix = ' . (!empty( $suffix ) ? '($new_str_len===$sublen?"'.$suffix.'":"")' : '""' ) . ';
			return ">" . @htmlspecialchars($new_str, ENT_QUOTES, "UTF-8") . "$suffix<";';
	
	return preg_replace( array( "#<[^/][^>]+>(?R)*</[^>]+>#", "#(<(b|h)r\s?/?>){2,}$#is"), "", trim( rtrim( ltrim( preg_replace_callback( "#>([^<]+)<#", create_function(
            '$a', $f ), ">$s<"  ), ">"), "<" ) ) );
}

function str_startsWith($haystack, $needle) {
  return strpos($haystack, $needle) === 0;
}

function str_endsWith($haystack, $needle){
    return strrpos($haystack, $needle) === strlen($haystack)-strlen($needle);
}

function str_contains($haystack, $needle) {
  return strpos($haystack, $needle) !== false;
}